import sys
from xml.dom.minidom import parse
from openpyxl import load_workbook


# TODO: 入口开发，支持参数选择功能

def is_key_column_exist(excel, sheet_name):
    wb = load_workbook(excel)
    ws = wb.get_sheet_by_name(sheet_name)
    return ws['A1'].value == 'key'


# def inset_key_to_translation_file():


if __name__ == '__main__':
    translation_file = sys.argv[1]
    sheet_name = sys.argv[2]
    merge_dir = sys.argv[3]
    print(translation_file, sheet_name, merge_dir)

    if not is_key_column_exist(translation_file, sheet_name):
        wb = load_workbook(translation_file)
        ws = wb.get_sheet_by_name(sheet_name)
        def_col_strings = []
        for cell in ws['A']:
            def_col_strings.append(cell.value)
        print(def_col_strings)

        def_string_file = merge_dir + '/values/strings.xml'
        dom = parse(def_string_file)
        string_nodes = dom.getElementsByTagName('string')
        string_dict = {}
        for node in string_nodes:
            for item in node.childNodes:
                name = node.getAttribute('name')
                value = item.nodeValue
                if value in def_col_strings:
                    string_dict[name] = value
        print(string_dict)

        # 写入excel
